<?php
/**
 * @Author:      lyt
 * @DateTime:    2016-05-03 11:23:11
 * @Description: 门店的库存盘点记录
 */
class MaintenanceInventoryList extends Eloquent
{
    protected $table = "maintenance_inventory_list";

    

    public static $status = array(
        "0"=>"新建",
        "1"=>"确认",
        "2"=>"成功",
        "3"=>"删除",
    );  
    
    /**
     * @Author:      lyt
     * @DateTime:    2016-05-11 17:39:13
     * @Description: 门店是否有 盘点正在进行
     */    
    public static function isInventory($shop_id){
        // $mil=MaintenanceInventoryList::where("shop_id",$shop_id)->whereIn("status",array(0,1))->first();
        // if($mil){//说明还有没完成的 库存盘点表
        //    return true;
        // }
        return false;     
    }


    public static function implement($mil_id,$user_id){
    	return DB::transaction(function () use ($mil_id,$user_id) {
            $msg='ok';
            $time=date('Y-m-d H:i:s',time());
            $supply_array=array();
    		$mil=MaintenanceInventoryList::where("id",$mil_id)->lockForUpdate()->first();
    		if($mil->status!=1){    
                $response = Lang::get("scs_error.data_inventory_status_error_61008");
                break;
            }
            $mis=MaintenanceInventorySupply::where("list_id",$mil->id)->where("status",1)->get();//获取此次盘点 盘点的耗材信息
            foreach ($mis as $_mis) {
            	$mw=MaintenanceWarehouse::where("shop_id",$_mis->shop_id)->where("item_id",$_mis->item_id)->first();
                //检查 耗材是否出入库过 
                $temp=DB::select("SELECT * FROM `maintenance_inventory_storage` mis LEFT JOIN maintenance_storage_item msi on mis.storage_id=msi.id
                    where mis_id=".$_mis->id." and mis.date_number != msi.current_number and mis.status=0");
                if(sizeof($temp)>0){//如果 大于零 说明 这个耗材的批次 有过出入库记录
                    $supply_array[]=array("item_id"=>$_mis->item_id,"item_type"=>$_mis->item_type);
                    MaintenanceInventoryStorage::where("mis_id",$_mis->id)->delete();
                    $_mis->delete();
                    continue;
                }
            	$miso=MaintenanceInventoryStorage::where("mis_id",$_mis->id)->where("status",0)->get();//获取该该耗材 盘点的耗材批次
            	foreach ($miso as $_miso) {
            		$msi=MaintenanceStorageItem::find($_miso->storage_id);	//获取修改的 耗材批次
            		$msi->current_number=$msi->current_number+$_miso->number;
            		$msi->save();
            		$_miso->status=1;
                    $_miso->implement_by=$user_id;
                    $_miso->implement_time=$time;
            		$_miso->save();
            	}
            	// 修改库存的数量 
                $mw->number=$mw->number+$_mis->number;
                $mw->save();
                //添加库存的修改记录
                $mwf=new MaintenanceWarehouseFlow;
                $mwf->mw_id=$mw->id;
                $mwf->type=3;//库存盘点
                $mwf->item_id=$_mis->id;
                $mwf->number=$_mis->number;
                $mwf->created_by=$user_id;
                $mwf->save();
                //修改 盘点表的状态
                $_mis->status=2;
                $_mis->implement_by=$user_id;
                $_mis->implement_time=$time;
                $_mis->save();
            }
            $mil->status=2;
            $mil->implement_by=$user_id;
            $mil->implement_time=$time;
    		$mil->save();
            //如果 这个盘点中有出入库执行 生成的新盘点单
            if(sizeof($supply_array)>0){
                $mil_new=new MaintenanceInventoryList;
                $new_id=Common::createId();
                $mil_new->id=$new_id;
                $mil_new->shop_id=$mil->shop_id;
                $mil_new->date=$mil->date;
                $mil_new->date_id=$mil->date_id;
                $mil_new->status=0;
                $mil_new->cust_id=$mil->cust_id;
                $mil_new->desc=$mil->id."盘点未完，自动生成盘点记录";
                $mil_new->create_by=$mil->create_by;
                $mil_new->save();
                foreach ($supply_array as $_sa) {
                    $count=MaintenanceStorageItem::where("shop_id",$mil_new->shop_id)->where("status",1)->where("type",1)->where("data_id",$mil_new->date_id)->where("current_number",">",0)->where("item_id",$_sa["item_id"])->sum("current_number");
                    $mis=new MaintenanceInventorySupply;
                    $mis->list_id=$new_id;
                    $mis->shop_id=$mil_new->shop_id;
                    $mis->item_type=$_sa["item_type"];
                    $mis->item_id=$_sa["item_id"];
                    $mis->date_number=$count;
                    $mis->current_number=0;
                    $mis->number=0-$count;
                    $mis->check_number=0-$count;
                    $mis->status=0;
                    $mis->type=1;
                    $mis->cust_id=$mil_new->cust_id;
                    $mis->date_id=$mil_new->date_id;
                    $mis->create_by=$mil_new->create_by;
                    $mis->save();
                }
            }



            return array('code' => 0, 'msg' => $msg, 'result' => array("id"=>$mil->id,"new_id"=>isset($new_id)?$new_id:0));
        });
    }




}